﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLDV.Models;

namespace QLDV.Controllers
{
    public class DoanVienController : BaseController
    {
        //
        // GET: /DoanVien/

        public ActionResult Index()
        {
            return View(Repository.LayDoanVien(Int32.Parse(CurrentUser.TenDangNhap)));
        }

        [HttpGet]
        public ActionResult ViewInfo(int MSSV = 08520090)
        {
            DOANVIEN dv = Repository.LayDoanVien(MSSV);
            IEnumerable<GIADINH> gddv = dv.GIADINHs;
            IEnumerable<QUATRINHCONGTAC> qtct = dv.QUATRINHCONGTACs;
            IEnumerable<NHANXET> nxdv = dv.NHANXETs;
            CSD csd = dv.CSD_DV2.Where(u => u.TrangThai == true && u.CSD.LOAICSD.CoChinhThuc==true).First().CSD;
            ViewBag.gddv = gddv;
            ViewBag.qtct = qtct;
            ViewBag.nxdv = nxdv;
            ViewBag.csd = csd;
            TempData["Iddv"] = dv.Id_Dv;
            return View(dv);
        }

        [HttpPost]
        public ActionResult ViewInfo(FormCollection values, DOANVIEN dv)
        {
            int Iddv = (int)TempData["Iddv"];
            string nst = values["NgaySinh"];
            List<int> lgd = new List<int>();
            List<int> lgdmoi = new List<int>();
            List<int> lgdxoa = new List<int>();
            foreach (string s in values.AllKeys)
            {
                if (s.Length > 9)
                {
                    if (s.Substring(0, 9) == "QuanHeMoi")
                        lgdmoi.Add(Int32.Parse(s.Substring(9)));
                }
                else
                {
                    if (s.Length > 6)
                        if (s.Substring(0, 6) == "QuanHe")
                            lgd.Add(Int32.Parse(s.Substring(6)));
                }
            }
            DOANVIEN dvien = Repository.LayDoanVien(Iddv);
            foreach (GIADINH gd in dvien.GIADINHs)
            {
                if (!lgd.Contains(gd.Id_Dv_Gd))
                {
                    lgdxoa.Add(gd.Id_Dv_Gd);
                }
            }
            foreach (int i in lgdxoa)
            {
                Repository.XoaGiaDinh(i);
            }
            foreach (int i in lgdmoi)
            {
                GIADINH gd = new GIADINH();
                gd.QuanHe = values["QuanHeMoi" + i];
                gd.HoTen = values["HoTenMoi" + i];
                gd.NoiO = values["NoiOMoi" + i];
                gd.NamSinh = Int32.Parse(values["NamSinhMoi" + i]);
                gd.NgheNghiep = values["NgheNghiepMoi" + i];
                gd.SoDT = values["SoDTMoi" + i];
                gd.Id_Dv = Iddv;

                Repository.ThemGiaDinh(gd);
            }
            foreach (int i in lgd)
            {
                GIADINH gd = new GIADINH();
                gd.QuanHe = values["QuanHe" + i];
                gd.HoTen = values["HoTen" + i];
                gd.NoiO = values["NoiO" + i];
                gd.NamSinh = Int32.Parse(values["NamSinh" + i]);
                gd.NgheNghiep = values["NgheNghiep" + i];
                gd.SoDT = values["SoDT" + i];

                Repository.CapNhatGiaDinh(i,gd);
            }

            dv.NgaySinh = new DateTime(Int32.Parse(nst.Substring(6, 4)), Int32.Parse(nst.Substring(3, 2)), Int32.Parse(nst.Substring(0, 2)));
            Repository.CapNhatDV(Iddv,dv);

            

            return RedirectToAction("Index", "Home");
        }
        
    }
}
